package yjj.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yjj.system.entity.Department;
import yjj.system.entity.Tenant;
import yjj.system.mapper.DepartmentMapper;
import yjj.system.model.TenantDeptDTO;
import yjj.system.service.DepartmentService;
import yjj.system.service.TenantService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department>  implements DepartmentService {

    @Autowired
    TenantService tenantService;


    @Override
    public List<TenantDeptDTO> listCombina() {
        //定义返回值
        List<TenantDeptDTO> tenantDeptDTOS = new ArrayList<>();
        //先查询租户列表集合
        List<Tenant> tenants = tenantService.list();
        for (Tenant tenant : tenants) {
            TenantDeptDTO tenantDTO = new TenantDeptDTO();
            tenantDTO.setId(tenant.getTenantId());
            tenantDTO.setName(tenant.getTenantName());
            tenantDTO.setLogo(tenant.getLogo());
            tenantDTO.setDomain(tenant.getDomain());
            // 获取该租户下的所有部门
            List<Department> departments = listByParentId(tenant.getTenantId());
            List<TenantDeptDTO> deptDTOList = new ArrayList<>();
            for (Department dept : departments) {
                TenantDeptDTO deptDTO = new TenantDeptDTO();
                deptDTO.setId(dept.getDeptId());
                deptDTO.setName(dept.getDeptName());
                deptDTO.setParentId(dept.getParentId());
                deptDTO.setDeptCode(dept.getDeptCode());
                deptDTO.setDeptSort(dept.getDeptSort());
                deptDTO.setDeptStatus(dept.getDeptStatus());
                deptDTO.setManagerId(dept.getManagerId());
                deptDTO.setPhone(dept.getPhone());
                deptDTO.setEmail(dept.getEmail());
                deptDTO.setFax(dept.getFax());
                // 递归查询子部门
                deptDTO.setChildren(getChildren(dept.getDeptId()));
                deptDTOList.add(deptDTO);
            }
            tenantDTO.setChildren(deptDTOList);
            tenantDeptDTOS.add(tenantDTO);
        }

        return tenantDeptDTOS;
    }

    /**
     * @param deptId 父级部门ID
     * @return
     */
    private List<TenantDeptDTO> getChildren( String deptId) {
        List<Department> subDeparts = listByParentId(deptId);
        List<TenantDeptDTO> deptDTOList = new ArrayList<>();
        for (Department dept : subDeparts) {
            TenantDeptDTO deptDTO = new TenantDeptDTO();
            deptDTO.setId(dept.getDeptId());
            deptDTO.setName(dept.getDeptName());
            deptDTO.setParentId(dept.getParentId());
            deptDTO.setDeptCode(dept.getDeptCode());
            deptDTO.setDeptSort(dept.getDeptSort());
            deptDTO.setDeptStatus(dept.getDeptStatus());
            deptDTO.setManagerId(dept.getManagerId());
            deptDTO.setPhone(dept.getPhone());
            deptDTO.setEmail(dept.getEmail());
            deptDTO.setFax(dept.getFax());

            deptDTOList.add(deptDTO);
        }

        return deptDTOList;
    }

    public List<Department> listByParentId(String tenantId) {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Department::getParentId, tenantId).eq(Department::getDelFlag, 0);
        return list(queryWrapper);
    }
}
